import { request } from "../../requests/post.js";

var app = getApp();

Component({
  data: {
    record: {},
    userInfo:[],
    // indexValue: ""
    
  },
  properties: {
    date: String,
    isShowModal: Boolean,
    indexUserId: Number,
    unit: String,
    recordList: {
      type: Array,
      value: []
    }
    // indexValue: String
  },
  methods: {
    formatTime(date) {
      var year = date.getFullYear();
      var month = date.getMonth() + 1
      month = month > 10 ? month : '0' + month;
      var day = date.getDate()
      day = day > 9 ? day : '0' + day;
      return year + '-' + month + '-' + day;
    },
    // 前一天 
    beforeBnt: function () {
      let Arr = this.data.date.split("-");
      var now = new Date(Number(Arr['0']), (Number(Arr['1']) - 1), Number(Arr['2']))
      now.setDate(now.getDate() - 1)
      now = this.formatTime(now)
      this.setData({
        date: now
      })

      this.setRecord( now, this)
    },
    
    // 后一天
    afterBtn: function () {
      let Arr = this.data.date.split("-");
      var now = new Date(Number(Arr['0']), (Number(Arr['1']) - 1), Number(Arr['2']))
      now.setDate(now.getDate() + 1)
      now = this.formatTime(now)
      this.setData({
        date: now
      })

      this.setRecord( now, this)
    },
    inputValue(e) {
      var record = this.data.record
      record.indexValue = e.detail.value
      this.setData({
        record:  record
      })
    },
    addRecord(){
      let that = this
      request({
        url: '/wx/insert_one_record',
        indexUserId: that.data.indexUserId,
        recordTime: that.data.date,
        indexValue: that.data.record.indexValue,
        id: that.data.record.id
      }).then(result => {
        if(result.data.errorCode === 200){
          that.setData("record", result.data.data)
          that.closeModal()
        }
      })
    },
    closeModal(){
      this.triggerEvent('onMyModal')
    },
    selectDete() {

    },
    bindDateChange(e) {
      let date = e.detail.value
      this.setData({
        date: date
      })
      this.getap
      this.setRecord( date, this)
    },

    setRecord( date, that){
      this.setData({
        record: {}
      })
      for(let r of this.data.recordList) {
        if(r.recordTime === date) {
            this.setData({
              record: r
            })
            return
        }
      }

    }
  },
  pageLifetimes: {
    show(){
      console.log("---------------- modal ---------------" )
      console.log("modal - " + JSON.stringify(this.data.recordList))
    }
  }
})
